// pages/profile/interests-skills/index.ts
import tran from '../../../utils/language';
import { commonTagList, userInfoStepThree } from '../../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: tran._t('interestsSkills'),
    _tBase: tran._t('base'),
    checked: [''],
    interestList: [
      {
        selectFlag: false,
        tagName: "",
        tagType: "",
        tagTypeId: ""
      }
    ],
    professionList: [
      {
        selectFlag: false,
        tagName: "",
        tagType: "",
        tagTypeId: ""
      }],
      loginInfo: {
        interestIds: [],
        skills: [],
      },
    isEdit: getApp().globalData.profileFrom === 'edit',
    nextFlag: false,
    registerFlag: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    const loginInfo = wx.getStorageSync('userInfo')
    const interestList = wx.getStorageSync('interestList')
    const professionList = wx.getStorageSync('professionList')
    if (!interestList || interestList.length <= 0) {
      this.getInterest()
    }
    if (!professionList || professionList.length <= 0) {
      this.getProfession()
    }
    this.setData({
      interestList,
      professionList,
      loginInfo,
      registerFlag: options.type
    }, () => {
      this.handleData()
    })
  },
  // 上一步
  previousTap () {
    // wx.navigateBack()
    wx.navigateTo({
      url: "/pages/profile/personalization/index?type=register"
    })
  },
  /**
   * 处理编辑的时候回显的数据
   */
  handleData () {
    const { interestList, professionList, loginInfo } = this.data
    const userInterest: any = loginInfo.interestIds
    const userProfession: any = loginInfo.skills
    interestList.map((item: any) => {
      if (userInterest && userInterest.length > 0) {
        const list = userInterest.filter((item1: any) => item1 == item.tagTypeId)
        if (list.length > 0) {
          item.selectFlag = true
        }
      }
    })
    professionList.map((item: any) => {
      if (userProfession && userProfession.length > 0) {
        const list = userProfession.filter((item1: any) => item1.tagTypeId == item.tagTypeId)
        if (list.length > 0) {
          item.selectFlag = true
        }
      }
    })
    this.setData({
      interestList,
      professionList
    }, () => {
      this.setInterestAndSkill('2')
      this.checkSelectNum()
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const interestList = wx.getStorageSync('interestList')
    const professionList = wx.getStorageSync('professionList')
    if (interestList && interestList.length > 0) {
      this.setData({
        interestList
      }, () => {
        this.handleData()
        this.checkSelectNum()
      })
    }
    if (professionList && professionList.length > 0) {
      this.setData({
        professionList
      }, () => {
        this.handleData()
        this.checkSelectNum()
      })
    }
  },
  confirmTap () {
    const { interestList, professionList, registerFlag } = this.data
    const currentInterestList = interestList.filter(item => item.selectFlag == true)
    const currentProfessionList = professionList.filter(item => item.selectFlag == true)
    if (currentInterestList.length < 3) {
      wx.showToast({
        title: '至少选择三种兴趣',
        icon: 'none'
      })
      return false
    }
    if (currentProfessionList.length < 3) {
      wx.showToast({
        title: '至少选择三种技能',
        icon: 'none'
      })
      return false
    }
    let currentInterestId: any = []
    let currentProfessionId: any = []
    currentInterestList.length > 0 && currentInterestList.map(item => {
      currentInterestId.push(item.tagTypeId)
    })
    currentProfessionList.length > 0 && currentProfessionList.map(item => {
      currentProfessionId.push(item.tagTypeId)
    })
    const params = {
      interestIds: currentInterestId,
      skillIds: currentProfessionId
    }
    wx.setStorageSync('currentProfessionList', currentProfessionList)
    this.setData({
      nextFlag: false
    })
    userInfoStepThree(params).then((res: any) => {
      // 因为请求里面都判断了状态码，所以走到这里，就是成功了
      // wx.showToast({
      //   title: '操作成功'
      // })
      let loginInfo = wx.getStorageSync('loginInfo')
      if (loginInfo.completedStep <= 3) {
        loginInfo.completedStep = 3
        wx.setStorageSync('loginInfo', loginInfo)
      }
      // getApp().getUserInfo()
      wx.navigateTo({
        url: `/pages/profile/proficiency/index?type=${registerFlag}`
      })
      setTimeout(() => {
        this.setData({
          nextFlag: true
        })
      }, 1000)
    })
  },
  /**
   * 获取兴趣列表
   */
  getInterest() {
    this.setData({
      interestList: []
    })
    let params = {
      tagType: 'INTEREST'
    }
    commonTagList(params).then(res => {
      res.length > 0 && res.map((item: any) => {
        item.selectFlag = false
      })
      this.setData({
        interestList: res
      }, () => {
        this.handleData()
      })
    })
  },
  /**
   * 获取职业技能列表
   */
  getProfession() {
    this.setData({
      professionList: []
    })
    let params = {
      tagType: 'SKILL'
    }
    commonTagList(params).then(res => {
      res.length > 0 && res.map((item: any) => {
        item.selectFlag = false
      })
      this.setData({
        professionList: res
      }, () => {
        this.handleData()
      })
    })
  },
  onChecke(e: baseEvent) {
    const value: string = e.target.dataset.value
    let newArr = this.data.checked
    const includes = this.data.checked.includes(value)
    if (includes) {
      newArr = newArr.filter(i => i !== value);
    } else {
      newArr.push(value);
    }
    this.setData({ checked: newArr })
  },
  changeProfessionSelect(e: any) {
    const { info } = e.currentTarget.dataset
    const { professionList } = this.data
    // 判断用户点击的是哪一个按钮，从而将那一个按钮状态反选
    professionList.map(item => {
      if (item.tagName === info.tagName) {
        item.selectFlag = !item.selectFlag
      }
    })
    this.setData({
      professionList
    }, () => {
      this.checkSelectNum()
    })
  },
  // 将选中的放进缓存里
  setInterestAndSkill (type: any, ) {
    // 1 是兴趣    2 是技能
    if (type == '1') {
      wx.setStorageSync('interestList', this.data.interestList);
    } else {
      wx.setStorageSync('professionList', this.data.professionList);
    }
  },
  changeInterestSelect(e: any) {
    const { info } = e.currentTarget.dataset
    const { interestList } = this.data
    // 判断用户点击的是哪一个按钮，从而将那一个按钮状态反选
    interestList.map(item => {
      if (item.tagName === info.tagName) {
        item.selectFlag = !item.selectFlag
      }
    })
    this.setData({
      interestList
    }, () => {
      this.setInterestAndSkill('1')
      this.checkSelectNum()
    })
  },
  checkSelectNum () {
    const { interestList, professionList } = this.data
    const currentInterestList = interestList.filter(item => item.selectFlag == true)
    const currentProfessionList = professionList.filter(item => item.selectFlag == true)
    if (currentInterestList.length >= 3 && currentProfessionList.length >= 3) {
      this.setData({
        nextFlag: true
      })
    } else {
      this.setData({
        nextFlag: false
      })
    }
  },
  goInterest() {
    wx.navigateTo({
      url: "/pages/profile/allInterest/index?type=interest"
    })
  },
  goSkill() {
    wx.navigateTo({
      url: '/pages/profile/allInterest/index?type=skill'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})